package kaufman.id.projects;

import java.util.Arrays;
import java.util.Random;

public class NinjaAssassin {
	private String[] players;
	private String[] targets;
	static Random random = new Random();

	/**
	 * Constructor for pairings of the game Ninja Assassin
	 * 
	 */

	public NinjaAssassin() {
	}

	public NinjaAssassin(String[] players) {

		this.players = players;
	}

	public String[] getTargets() {
		this.targets = Arrays.copyOf(players, players.length);
		rotateRight(targets);
		for (int i = 0; i < 43; i++) {// 42 possible permutations of 7nPr2
			swap(targets, players);
		}

		return targets;

	}

	public void printplayers() {
		for (int i = 0; i <= players.length - 1; i++) {
			System.out.println(players[i]);
		}
	}

	/**
	 * 
	 * @param targets
	 */
	private void rotateRight(String[] targets) {// rotates targets to the
												// right one place
		String pre = targets[targets.length - 1];
		String current;

		for (int r = 0; r < targets.length; r++) {
			current = targets[r];// =DR
			targets[r] = pre;
			pre = current;
		}
	}

	/**
	 * 
	 * @param targets
	 * @param players
	 */
	private void swap(String[] targets, String[] players) {// psuedorandomly
															// swaps
															// targets once
		while (true) {
			int Index1 = random.nextInt(players.length - 1);

			int Index2 = random.nextInt(targets.length - 1);
			String tar1 = targets[Index1];
			String tar2 = targets[Index2];
			String play1 = players[Index1];
			String play2 = players[Index2];
			if (tar1 != play2 && tar2 != play1) {
				// used toe check values if swapped, no clashes
				targets[Index1] = tar2;
				targets[Index2] = tar1;
				break;
			}
		}

	}

	/**
	 * 
	 * @param players
	 * @param targets
	 */
	private void repeatingNameCheck(String[] players) {// checks for
														// repeating
														// names and
														// terminates if
														// so
														// while(true){
		for (int i = 0; i <= players.length - 1; i++) {
			for (int y = 0; y <= players.length - 1; y++) {
				if (players[i].equals(players[y]) && i!=y) {
					System.exit(0);
				}
			}
		}
	}
}